 /*
 * UMLSynch - VPSDE plugin part
 * http://umlsynch.googlecode.com
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */

package com.googlecode.umlsynch.vpsde.rpc_old.VPOperationServer;

import com.googlecode.umlsynch.vpsde.rpc.VPSDEDiagramManager.VPClassDiagramManager;
import com.googlecode.umlsynch.vpsde.rpc.VPSDEDiagramManager.VPPackageDiagramManager;
import com.vp.plugin.ApplicationManager;
import com.vp.plugin.diagram.IDiagramElement;
import com.vp.plugin.diagram.IDiagramUIModel;
import com.vp.plugin.model.IState2;
import com.vp.plugin.model.factory.IModelElementFactory;

public class TVPOperationServerImplementation2 implements
		IVPOperationServer {

	
	public TVPOperationServerImplementation2() {
	}

	@Override
	public void CreateClassOnDiagram(VPSDEClassDescriptor desc, VPPoint pt) {
		ApplicationManager.instance().getViewManager().showMessage("CreateClassOnDiagram");
		IDiagramUIModel diagramModel = ApplicationManager.instance().getDiagramManager().getActiveDiagram();
		IDiagramElement diagramElement = 
			VPClassDiagramManager.instance().AddClassToDiagram(diagramModel, desc.className);
				
		if (diagramElement != null) {		
			VPClassDiagramManager.instance().
			SetClasPropery(diagramElement.getModelElement(), 
					VPClassDiagramManager.PROP_CLASS_FILE_NAME, desc.classFileName);

		}
	}

	@Override
	public void CreatePacketOnDiagram(VPSDEPacketDescriptor desc, VPPoint pt) {
		
		ApplicationManager.instance().getViewManager().showMessage("CreatePacketOnDiagram");
		
		IDiagramUIModel mDiagram = ApplicationManager.instance().getDiagramManager().getActiveDiagram();
		
		VPPackageDiagramManager vpack = VPPackageDiagramManager.instance();		
		
		
		IDiagramElement de = vpack.AddPackageToDiagram(mDiagram, desc.PacketName);
		vpack.SetProperty(de, vpack.PROJECT_NAME, desc.ProjectName);
		vpack.SetProperty(de, vpack.PACKAGE_PATH, desc.PacketPath);
		
		String packPaths = null;		
		if (desc.PacketDependencyPaths != null) {
			for (int i=0; i < desc.PacketDependencyPaths.length; ++i) {
				packPaths += desc.PacketDependencyPaths[0] + ";";
			}
			vpack.SetProperty(de, vpack.PACKAGE_DEPENDENCY, packPaths);
		}

		
		//de.setX(100);
//		de.setY(100);
		//de.setForeground(Color.BLACK);
		//ApplicationManager.instance().getViewManager().getRootFrame().getToolkit().sync();
/*
		IPackage sPackage = IModelElementFactory.instance().createPackage(); 
		sPackage.setName(desc.PacketName);				
		IDiagramElement ddd = ApplicationManager.instance().getDiagramManager().
		          createDiagramElement(mDiagram, sPackage);
		ddd.getModelElement().setName(desc.PacketName);
		
		ddd.setX(100);
		ddd.setY(100);
		ddd.setZOrder(0);
		ddd.setWidth(40);
		ddd.setHeight(60);
		
		*/		
	}

	@Override
	public void CreateEnumOnDiagram(VPSDEEnumDescriptor desc, VPPoint pt) {
		ApplicationManager.instance().getViewManager().showMessage("CreateEnumOnDiagram");
		
		IDiagramUIModel mDiagram = ApplicationManager.instance().getDiagramManager().getActiveDiagram();
		mDiagram.setName(desc.EnumName);
		ApplicationManager.instance().getViewManager().showMessage("Number of fields:" + desc.Fields.length);
		for (int t=0; t<desc.Fields.length; ++t) {
			ApplicationManager.instance().getViewManager().showMessage("Field:" + desc.Fields[t]); 
			IState2 state = IModelElementFactory.instance().createState2();
			state.setName(desc.Fields[t]);
			IDiagramElement diagramElement = ApplicationManager.instance().getDiagramManager().
			createDiagramElement(mDiagram, state);
			diagramElement.getModelElement().setName(desc.Fields[t]);

			diagramElement.setX(100);
			diagramElement.setY(100);
			diagramElement.setZOrder(0);
			diagramElement.setWidth(40);
			diagramElement.setHeight(60);
		}		
		ApplicationManager.instance().getViewManager().getRootFrame().getToolkit().sync();	
	}
	
	@Override
	public VPWorkingAreaDescriptor GetWorkingArea() {
		ApplicationManager.instance().getViewManager().showMessage("GetWorking Area");
		VPWorkingAreaDescriptor result = new VPWorkingAreaDescriptor();

		IDiagramUIModel mDiagram = ApplicationManager.instance().getDiagramManager().getActiveDiagram();

		if (mDiagram == null) {
			ApplicationManager.instance().getViewManager().showMessage("return null");
			return null;
		}
		
		// Return the active diagram Info
		result.X = mDiagram.getX();
		result.Y = mDiagram.getY();
		result.Width = mDiagram.getWidth();
		result.Height = mDiagram.getHeight();
		result.DiagramType = mDiagram.getType();
		ApplicationManager.instance().getViewManager().showMessage("return: " + result.X + " Y:" +
				result.Y + " Width:" + result.Width + " Heigh:" + result.Height 
				+ result.DiagramType);
		return result;
	}
}
